/*
 * @Author: your name
 * @Date: 2021-11-22 16:02:33
 * @LastEditTime: 2024-02-29 17:26:50
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \agricultural-production-web\src\api\axios.js
 */
/*
 *@descrion:此封装方法主要时用户的登录，与选择农场页面
 */
import axios from 'axios';
import { Message } from 'element-ui';
import { getToken } from '@/utils/auth';
import store from '@/store';
import baseUrl from '@/api/baseURL';
const zhaxios = axios.create({
  // baseURL: "http://ia.senontech.com/",

  baseURL: baseUrl.webServiceUrl,
  // baseURL: "http://192.168.1.85:9090/production/",
  // baseURL: "http://192.168.1.97:9090/",
  timeout: 40000 // 请求超时时间
});

// request interceptor
zhaxios.interceptors.request.use(
  config => {
    // 这个玩意现在登录加上去就可以登录
    config.headers["sysDataBaseName"] = "DB_TDW1_SC";
    config.headers["farming"] = getToken("farming") || "PLANT";
    if (getToken("token")) {
      config.headers["accessToken"] = getToken("token");
    }
    if (getToken('token')) {
      // config.headers["systemMark"] = "SC";
      config.headers['databaseName'] = getToken('databaseName');
      // config.headers['systemMark'] = 'MN' || getToken('virtualSystemNo') || 'zhiyang';
      config.headers['organizationList'] = getToken('organizationList');
      config.headers['organizationNo'] = getToken('organizationNo');
      config.headers['virtualSystemNo'] = getToken('virtualSystemNo');
      config.headers['farming'] = getToken('farming') || 'PLANT';
      const systemMarkMap = {
        FISH: 'SC',
        BREED: 'ZY',
        PLANT: 'MN'
      };
      config.headers['systemMark'] = systemMarkMap[config.headers['farming']];
    }
    if (getToken('lesseeNo')) {
      config.headers['lesseeNo'] = getToken('lesseeNo');
    }
    if (config.url.includes('/employee/downExcel')) {
      // 导入文件设置请求头
      config.headers['responseType'] = 'blob';
    }
    if (config.url.includes('/employee/importExcel')) {
      // 导入文件设置请求头
      config.headers['Content-Type'] = 'multipart/form-data';
    }
    if (config.url.includes('/web/login')) {
      // 导入请求头
      config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
    }
    config.cancelToken = new axios.CancelToken(cancel => {
      store.commit('permission/add_AxiosPromiseArr', { cancel });
    });
    // 全局参数 start 湖羊进销存接口
    if (config.url.includes('/queryPageByCondition')) {
      if (config.data !== undefined) {
        if (config.data.entity !== undefined) {
          config.data.entity.orgainzationMark = getToken('organizationNo') || 'YS';
          config.data.entity.organizationNo = getToken('organizationNo');
          config.data.entity.systemNo = 'MN' || getToken('virtualSystemNo');
        }
      }
    }
    if (!config.url.includes('/login')) {
      if (config.data !== undefined) {
        if (config.data.entity !== undefined) {
          config.data.entity.organizationNo = getToken('organizationNo');
          if (!config.data?.entity.farming) config.data.entity.farming = getToken('farming') || 'PLANT';
          config.headers['Content-Type'] = 'application/json';
        } else {
          config.data.organizationNo = getToken('organizationNo');
          config.data.farming = getToken('farming') || 'PLANT';
          config.data.systemNo = 'MN' || getToken('virtualSystemNo');
        }
      }
    }
    if (config.url.includes('/queryByCondition')) {
      if (config.data !== undefined) {
        config.data.organizationNo = getToken('organizationNo');
        config.data.systemNo = 'MN' || getToken('virtualSystemNo');
      }
    }
    if (config.url.includes('/measureRecordExport')) {
      if (config.data !== undefined) {
        config.data.organizationNo = getToken('organizationNo');
        config.data.systemNo = 'MN' || getToken('virtualSystemNo');
      }
    }
    if (config.url.includes('/add')) {
      if (config.data !== undefined && !config.data.organizationNo) {
        config.data.organizationNo = getToken('organizationNo');
      }
    }
    if (config.url.includes('/exportData')) {
      if (config.data !== undefined) {
        config.data.organizationNo = getToken('organizationNo');
      }
    }
    if (config.url.includes('/delByNo?') && !config.url.includes('&organizationNo')) {
      config.url = config.url + '&organizationNo=' + getToken('organizationNo');
    }
    if (config.url.includes('/queryByNo?') && !config.url.includes('&organizationNo')) {
      config.url = config.url + '&organizationNo=' + getToken('organizationNo');
    }
    if (config.url.includes('/delListByNo')) {
      if (config.data !== undefined) {
        config.data.organizationNo = getToken('organizationNo');
      }
    }
    // if (config.url.includes('/queryPageByCondition') || config.url.includes('/queryPageList')) {
    //   if (config.data !== undefined) {
    //     if (config.data.entity !== undefined) {
    //       config.data.entity.orgainzationMark = getToken('organizationNo') || 'YS';
    //       config.data.entity.organizationNo = getToken('organizationNo') || 'YS';
    //       config.headers['Content-Type'] = 'application/json';
    //     }
    //   }
    // }
    // 全局参数 end

    return config;
  },
  error => {
    console.log(error);
    return Promise.reject(error);
  }
);

// response interceptor
zhaxios.interceptors.response.use(
  response => {
    const res = response.data;
    if (![1, 200].includes(res.code)) {
      if (res.msg) {
        Message({
          message: res.msg,
          type: 'error',
          duration: 5 * 1000
        });
      }
    }
    return res;
  },
  error => {
    if (error.response) {
      const res = error.response.data;
      if (error.response.data.msg) {
        Message({
          message: error.response.data.msg,
          type: 'error',
          duration: 5 * 1000
        });
      } else {
        Message({
          message: '服务器异常',
          type: 'error',
          duration: 5 * 1000
        });
      }
      return Promise.reject(res);
    } else if (error.request) {
      Message({
        message: '服务器异常',
        type: 'error',
        duration: 5 * 1000
      });
    } else {
      Message({
        message: error.message,
        type: 'error',
        duration: 5 * 1000
      });
    }
    return Promise.reject(error);
  }
);

// 封装get请求
export function get(url, params = {}) {
  params.t = new Date().getTime(); // get方法加一个时间参数,解决ie下可能缓存问题.
  return zhaxios({
    url: url,
    method: 'get',
    params
  });
}

// 封装post请求
export function post(url, data = {}) {
  // 默认配置
  let sendObject = {
    url: url,
    method: 'post',
    data
  };
  console.log(url, 'post', data);

  sendObject.data = JSON.stringify(data);
  return zhaxios(sendObject);
}

export default zhaxios;
